package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_BalanceMapper;
import com.bc.bcapi.dao.T_Inner_transMapper;
import com.bc.bcapi.utils.web3j.ERC20TransactionModel;
import com.bc.bcapi.utils.web3j.Util;
import com.bc.bcmodel.domain.T_BalanceModel;
import com.bc.bcmodel.domain.T_Inner_transModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TransColdService {
    @Resource
    T_Inner_transMapper t_inner_transMapper;
    @Resource
    T_BalanceMapper t_balanceMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(TransColdService.class);

    /*
    1. T_Inner_transModel 更新状态
    2. t_balance 收入 y
    3. t_balance 支出矿工费
     */
    @Transactional(rollbackFor = {
            Exception.class}, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void transColdSuccess(T_Inner_transModel transColdModel) throws Exception {
        transColdModel.setPay_status(1);
        if (t_inner_transMapper.update(transColdModel) < 0)
            throw new Exception("update inner trans status error");

        List<T_BalanceModel> balanceModelList = new ArrayList<>();

        T_BalanceModel balanceModel = new T_BalanceModel();
        balanceModel.setToken_type(transColdModel.getToken_name());
        balanceModel.setTrans_type("收入");
        balanceModel.setTrans_id(transColdModel.getInner_trans_id());
        balanceModel.setValue(transColdModel.getWithdraw_account());
        balanceModelList.add(balanceModel);

        T_BalanceModel gasBalanceModel = new T_BalanceModel();
        gasBalanceModel.setToken_type(Util.getETHID());
        gasBalanceModel.setTrans_type("转冷GAS支出");
        gasBalanceModel.setTrans_id(transColdModel.getInner_trans_id());
        gasBalanceModel.setValue(transColdModel.getGas_account());
        balanceModelList.add(gasBalanceModel);

        if (t_balanceMapper.insertBatch(balanceModelList) <= 0)
            throw new Exception("insert balance error");
    }

    @Transactional(rollbackFor = {
            Exception.class}, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public void transColdFail(T_Inner_transModel transColdModel, ERC20TransactionModel transactionModel) {
        transColdModel.setPay_status(2);
        t_inner_transMapper.update(transColdModel);

        T_BalanceModel gasBalanceModel = new T_BalanceModel();
        gasBalanceModel.setToken_type(Util.getETHID());
        gasBalanceModel.setTrans_type("转冷GAS支出");
        gasBalanceModel.setTrans_id(transColdModel.getInner_trans_id());
        gasBalanceModel.setValue(transColdModel.getGas_account());
        t_balanceMapper.insert(gasBalanceModel);

    }
}
